//
//  JLCFVierticalScrollView.m
//  上下滚动btn
//
//  Created by 李杨 on 16/2/25.
//  Copyright © 2016年 liyang. All rights reserved.
//

#import "JLCFVierticalScrollView.h"

#define MMColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define MMRandomColor MMColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))
#define Screen_width ([UIScreen mainScreen].bounds.size.width)
#define Screen_height ([UIScreen mainScreen].bounds.size.height)
#define BTNWidth self.bounds.size.width
#define BTNHeight self.bounds.size.height
@interface JLCFVierticalScrollView ()
@property (nonatomic,strong) NSMutableArray *titles;
@property(assign, nonatomic)int titleIndex;
@property(assign, nonatomic)int index;
@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, assign) NSInteger selectIndex;

@end
@implementation JLCFVierticalScrollView

+ (instancetype)initWithTitleArray:(NSArray *)titles AndFrame:(CGRect)frame
{
    return [[self alloc] initWithArray:titles AndFrame:frame];
}

- (instancetype)initWithArray:(NSArray *)titles AndFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.selectIndex = 0;
        NSMutableArray *MutableTitles = [NSMutableArray arrayWithArray:titles];
        NSString *str = @"";
        self.titles = MutableTitles;
        [self.titles addObject:str];
        self.index = 1;
        UIButton *btn = [[UIButton alloc]init];
        btn.frame = CGRectMake(0, 0, BTNWidth, BTNHeight);
        btn.tag = self.index;
        btn.userInteractionEnabled = NO;
        [btn setTitle:self.titles[0] forState:UIControlStateNormal];
        [btn setTitleColor:QTZ_Gray73 forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self addSubview:btn];
        self.clipsToBounds = YES;
        weakify(self);
        [self bk_whenTapped:^{
            strongify(self);
            if ([self.delegate respondsToSelector:@selector(selectAtIndex:)]) {
                [self.delegate selectAtIndex:self.selectIndex];
            }
        }];
    }

    return self;
}

- (void)reloadData
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextButton) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)nextButton {
    
    self.selectIndex++;
    UIButton *firstBtn = [self viewWithTag:self.index];
    [firstBtn setTitleColor:QTZ_Gray73 forState:UIControlStateNormal];
    firstBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    UIButton *modelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, BTNHeight, BTNWidth, BTNHeight)];
    modelBtn.tag = self.index + 1;
    [modelBtn setTitleColor:QTZ_Gray73 forState:UIControlStateNormal];
    modelBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    modelBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    if ([self.titles[self.titleIndex+1] isEqualToString:@""]) {
        self.titleIndex = -1;
        self.index = 0;
        self.selectIndex = 0;
    }
    if (modelBtn.tag == self.titles.count) {
        
        modelBtn.tag = 1;
    }
    modelBtn.userInteractionEnabled = NO;
    
    [modelBtn setTitle:self.titles[self.titleIndex+1] forState:UIControlStateNormal];
    [self addSubview:modelBtn];

    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        firstBtn.top = -BTNHeight;
        modelBtn.top = 0;
    } completion:^(BOOL finished) {
        [firstBtn removeFromSuperview];
    }];
    
    self.index++;
    self.titleIndex++;
}

- (void)clickBtn:(UIButton *)btn {

    if ([self.delegate respondsToSelector:@selector(clickTitleButton:)]) {
        
        [self.delegate clickTitleButton:btn];
        
    }

}

@end
